#!/usr/bin/env node

const { DocumentManager } = require('./build/document-manager.js');
const { VectorSearchService } = require('./build/vector-search.js');
const fs = require('fs-extra');
const path = require('path');

async function testDocumentRAGSystem() {
  console.log('🔧 Document RAG System Test');
  console.log('=' .repeat(50));

  const documentManager = new DocumentManager();
  const vectorService = new VectorSearchService();
  
  try {
    // 清理现有数据
    console.log('🧹 Cleaning up existing data...');
    const metadataPath = path.join(process.cwd(), '.cb-document-helper');
    if (await fs.pathExists(metadataPath)) {
      await fs.remove(metadataPath);
    }
    
    // 清理向量数据库集合
    try {
      await vectorService.clearCollection();
      console.log('✅ Cleared vector database collection');
    } catch (error) {
      console.log('⚠️  Vector collection cleanup (expected if collection doesn\'t exist)');
    }

    console.log('\n=== Phase 1: Initial Document Processing ===');
    
    // 处理示例文档
    const projectPath = path.resolve('example-docs');
    console.log(`📂 Processing documents in: ${projectPath}`);
    
    const startTime = Date.now();
    const stats = await documentManager.refreshDocuments(projectPath, true);
    const duration = Date.now() - startTime;
    
    console.log(`\n📊 Processing Results:`);
    console.log(`  ⏱️  Total time: ${duration}ms`);
    console.log(`  📄 Total files: ${stats.totalFiles}`);
    console.log(`  ✅ Updated files: ${stats.updatedFiles}`);
    console.log(`  ⏭️  Skipped files: ${stats.skippedFiles}`);
    console.log(`  📝 Chunks created: ${stats.chunksCreated}`);
    console.log(`  📋 Briefings generated: ${stats.briefingsGenerated}`);
    
    console.log('\n=== Phase 2: Vector Search Testing ===');
    
    // 测试中文搜索
    const searchQueries = [
      '人工智能机器学习',
      'Python编程',
      '微服务架构',
      'TypeScript开发',
      '向量数据库'
    ];
    
    for (const query of searchQueries) {
      console.log(`\n🔍 Searching for: "${query}"`);
      
      const searchStart = Date.now();
      const results = await vectorService.searchChunks(query, 3);
      const searchDuration = Date.now() - searchStart;
      
      console.log(`  ⚡ Search time: ${searchDuration}ms`);
      console.log(`  📊 Results found: ${results.length}`);
      
      for (let i = 0; i < results.length; i++) {
        const result = results[i];
        const preview = result.content.substring(0, 80) + (result.content.length > 80 ? '...' : '');
        console.log(`    ${i + 1}. Score: ${result.score.toFixed(3)} - ${path.basename(result.filePath)}`);
        console.log(`       "${preview}"`);
      }
    }

    console.log('\n=== Phase 3: Incremental Update Testing ===');
    
    // 创建测试文档
    const testDocPath = path.join(projectPath, 'test-incremental.md');
    const testContent = `---
title: "测试增量更新"
author: "测试系统"
date: "2025-05-29"
---

# 测试增量更新

这是一个测试增量更新功能的文档。

## 内容块 1
这里是第一个内容块，包含一些关于**增量更新**的信息。

## 内容块 2
这里是第二个内容块，描述了**文档处理**的流程。
`;
    
    await fs.writeFile(testDocPath, testContent, 'utf-8');
    console.log('📝 Created test document');
    
    // 第一次处理
    console.log('\n🔄 First processing...');
    const stats1 = await documentManager.refreshDocuments(projectPath, false);
    console.log(`  Updated: ${stats1.updatedFiles}, Chunks: ${stats1.chunksCreated}`);
    
    // 等待1秒确保时间戳不同
    await new Promise(resolve => setTimeout(resolve, 1000));
    
    // 修改文档
    const modifiedContent = testContent + `\n## 新增内容块\n这是新增的内容块，用于测试增量更新。`;
    await fs.writeFile(testDocPath, modifiedContent, 'utf-8');
    console.log('✏️  Modified test document');
    
    // 第二次处理（应该检测到变更）
    console.log('\n🔄 Second processing (should detect changes)...');
    const stats2 = await documentManager.refreshDocuments(projectPath, false);
    console.log(`  Updated: ${stats2.updatedFiles}, Chunks: ${stats2.chunksCreated}`);
    
    // 第三次处理（应该跳过，因为没有变更）
    console.log('\n🔄 Third processing (should skip unchanged files)...');
    const stats3 = await documentManager.refreshDocuments(projectPath, false);
    console.log(`  Updated: ${stats3.updatedFiles}, Skipped: ${stats3.skippedFiles}`);

    console.log('\n=== Phase 4: Timestamp Verification ===');
    
    // 验证时间戳文件
    const timestampFile = path.join(process.cwd(), '.cb-document-helper', 'timestamps.json');
    if (await fs.pathExists(timestampFile)) {
      const timestamps = await fs.readJson(timestampFile);
      console.log('📅 Timestamp file exists');
      console.log(`   Records: ${Object.keys(timestamps).length}`);
      
      // 检查时间戳格式（应该是数字而不是字符串）
      const samplePath = Object.keys(timestamps)[0];
      const sampleTimestamp = timestamps[samplePath];
      const isNumber = typeof sampleTimestamp === 'number';
      console.log(`   Format: ${isNumber ? 'Number (✅)' : 'String (❌)'}`);
      
      if (isNumber) {
        const date = new Date(sampleTimestamp);
        console.log(`   Sample: ${sampleTimestamp} -> ${date.toISOString()}`);
      }
    } else {
      console.log('❌ Timestamp file not found');
    }

    console.log('\n=== Phase 5: Search Quality Assessment ===');
    
    // 测试搜索结果质量
    const testQuery = '增量更新';
    console.log(`🎯 Testing search quality for: "${testQuery}"`);
    
    const qualityResults = await vectorService.searchChunks(testQuery, 5);
    console.log(`📊 Found ${qualityResults.length} results`);
    
    let relevantResults = 0;
    for (const result of qualityResults) {
      const isRelevant = result.content.includes('增量') || result.content.includes('更新');
      if (isRelevant) relevantResults++;
      console.log(`  Score: ${result.score.toFixed(3)}, Relevant: ${isRelevant ? '✅' : '❌'}`);
    }
    
    const precision = qualityResults.length > 0 ? (relevantResults / qualityResults.length * 100).toFixed(1) : 0;
    console.log(`🎯 Search precision: ${precision}%`);

    // 清理测试文档
    await fs.remove(testDocPath);
    console.log('🧹 Cleaned up test document');

    console.log('\n✅ RAG Document System Test Complete!');
    console.log('Key features verified:');
    console.log('  ✅ Document processing and chunking');
    console.log('  ✅ Vector embedding generation');
    console.log('  ✅ Semantic search functionality');
    console.log('  ✅ Incremental update detection');
    console.log('  ✅ Timestamp-based file comparison');
    console.log('  ✅ Search quality and relevance');

  } catch (error) {
    console.error('❌ Test failed:', error);
    process.exit(1);
  }
}

if (require.main === module) {
  testDocumentRAGSystem().catch(console.error);
}
